﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace SMP.Client.PolForm
{
    using System.Collections;
    using SMP.Windows;
    using SMP.Service;
    using SMP.Domain;

    public partial class FrmThanhvien : SMP.Windows.Forms.FrmBase
    {
        Vaitro_Service _objVaitro = new Vaitro_Service();
        DataTable dtb_thanhvien = new DataTable();
        Vaitro thanhvien = new Vaitro();

        public FrmThanhvien()
        {
            InitializeComponent();
            item_Print.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            item_Select.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
        }

        #region DisplayInfo, ResetText, ChangeStatus, ClearDataBindings, DataBindingControl

        public override void DisplayInfo()
        {
            try
            {
                this.FormState = Windows.Forms.FormState.View;
                dtb_thanhvien = _objVaitro.GetAll_Thanhvien();
                dgThanhvien.DataSource = dtb_thanhvien;

                ClearDataBindings();
                ResetText();
                ChangeStatus();
                if (gvThanhvien.RowCount > 0)
                    DataBindingControl();

            }
            catch (Exception ex)
            {
                Windows.Forms.Message.Show(ex.Message, ex.ToString());
            }
        }

        public override void ResetText()
        {
            txtTaikhoan.EditValue = null;
            txtMatkhau.EditValue = null;
            txtMota.EditValue = null;
            txtHoten.EditValue = null;

            txtTaikhoan.Focus();
        }

        public override void ChangeStatus(bool editTable = true)
        {
            txtTaikhoan.Properties.ReadOnly = editTable;
            txtMatkhau.Properties.ReadOnly = editTable;
            if (this.FormState == Windows.Forms.FormState.Edit)
                txtMatkhau.Properties.ReadOnly = true;
            txtMota.Properties.ReadOnly = editTable;
            txtHoten.Properties.ReadOnly = editTable;
        }

        public override void ClearDataBindings()
        {
            txtTaikhoan.DataBindings.Clear();
            txtMatkhau.DataBindings.Clear();
            txtMota.DataBindings.Clear();
            txtHoten.DataBindings.Clear();
        }

        public override void DataBindingControl()
        {
            txtTaikhoan.DataBindings.Add("EditValue", dtb_thanhvien, ".Taikhoan");
            txtMatkhau.DataBindings.Add("EditValue", dtb_thanhvien, ".Matkhau");
            txtMota.DataBindings.Add("EditValue", dtb_thanhvien, ".Mota");
            txtHoten.DataBindings.Add("EditValue", dtb_thanhvien, ".Hoten");
        }

        #endregion


        #region PerformAdd, PerformEdit, PerformDelete, PerformCancel, PerformSave, PerformSelectOneObject

        public override bool PerformAdd()
        {
            ClearDataBindings();
            ResetText();
            ChangeStatus(false);
            return true;
        }

        public override bool PerformEdit()
        {
            ChangeStatus(false);
            return true;
        }

        public override bool PerformDelete()
        {
            var del = false;
            if(Windows.Forms.Message.Show("Bạn có thực sự muốn xóa "+txtHoten.EditValue+" ", "Thông Báo", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                del = _objVaitro.Delete_Thanhvien(gvThanhvien.GetFocusedRowCellValue("Id_Thanhvien"));
            if (del)
                DisplayInfo();
            return true;
        }

        public override bool PerformCancel()
        {
            ClearDataBindings();
            DisplayInfo();
            return true;
        }

        public override bool PerformSave()
        {
            var save = false;
            Hashtable hashtableControl = new Hashtable();
            hashtableControl.Add(txtMatkhau, lbMatkhau);
            hashtableControl.Add(txtTaikhoan, lbTaikhoan);
            hashtableControl.Add(txtHoten, lbHoten);
            if (!Windows.Utility.Validator.CheckNullFields(hashtableControl))
                return false;
            if (dtb_thanhvien != null && dtb_thanhvien.Rows.Count > 0)
            {
                DataRow[] row = dtb_thanhvien.Select(string.Format("Taikhoan = '{0}'", txtTaikhoan.EditValue));
                if (row.Length > 0)
                {
                    Windows.Forms.Message.Show("Tài khoan " + txtTaikhoan.EditValue + " đã tồn tại.\nVui lòng xem lại", "Thông Báo", MessageBoxButtons.OK);
                    return false;
                }
            }

            thanhvien.Taikhoan = txtTaikhoan.EditValue;
            thanhvien.Mota = txtMota.EditValue;
            thanhvien.Matkhau = Windows.Utility.Text.EncodeMD5(txtMatkhau.Text);
            thanhvien.Hoten = txtHoten.EditValue;
            thanhvien.Date_Create = DateTime.Now.Date;

            if (this.FormState == Windows.Forms.FormState.Add)
                save = _objVaitro.Insert_Thanhvien(thanhvien);
            else
                save = _objVaitro.Update_Thanhvien(thanhvien);
            if (save)
                DisplayInfo();

            return true;
        }
        
        #endregion

        
        private void gvThanhvien_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                if (e.Column.FieldName == "STT")
                    e.DisplayText = (Convert.ToInt64(e.RowHandle) + 1).ToString();
            }
        }

        


        



    }
}